/**
 *
 */
package de.cbf.cam.dice.entities;

import java.beans.IndexedPropertyChangeEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

/**
 * @author Surfer
 *
 */
public class RollDataOpen extends RollDataModifier {
	public static final int PropertyIndexResult			 = 6;

	protected Integer 	result		;

	public RollDataOpen() {
		super();
		result			= 0 ;
		propertyChange.addPropertyChangeListener(new PCLResult());
	}


	public Integer getResult() {
		return result;
	}

	public void setResult(Integer result) {
		boolean same = this.result.equals(result);
		this.result = result;
		if(! same){
			propertyChange.fireIndexedPropertyChange("Result",PropertyIndexResult,null,result);
		}
	}

	private class PCLResult implements PropertyChangeListener{
		@Override
		public void propertyChange(PropertyChangeEvent arg0) {
			if (arg0 instanceof IndexedPropertyChangeEvent) {
				IndexedPropertyChangeEvent indexedPCE = (IndexedPropertyChangeEvent) arg0;
				switch (indexedPCE.getIndex()) {
					case PropertyIndexRolls:{
						int result = 0;
						//get rolls
						int[] rolls = getRolls();
						for (int i = 0; i < rolls.length; i++) {
							result += rolls[i];
						}
						//add modifier
						result += getModifierValue();
						setResult(result);
						break;
					}
					case PropertyIndexModifiers:{
						int result = 0;
						//get rolls
						int[] rolls = getRolls();
						for (int i = 0; i < rolls.length; i++) {
							result += rolls[i];
						}
						//add modifier
						result += getModifierValue();
						setResult(result);
						break;
					}
				}
			}
		}
	}
}
